package com.bondqin.demo.controller.rest;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bondqin.demo.action.DailyRecordAction;
import com.bondqin.demo.controller.BaseController;
import com.bondqin.demo.entity.Employee;
import com.bondqin.demo.vo.DailyRecordVo;
import com.bondqin.demo.vo.ResultModel;


@RestController
@RequestMapping(value="/dailyRecord")
public class DailyRecordRest extends BaseController {
	
/**
 * @RequestMapping(value="/test")
   public String hello(Map<String, Object> model) {
      
      model.put("msg", "Hello");
      return "/test";
   }
 */
	@Autowired
	private DailyRecordAction dailyRecordAction;

	/**
	 * @param number	
	 * @param size	
	 * @param model	
	 * 
	 */
	@RequestMapping(value="listAll.json")
	public Map<String, Object> listAll(@RequestParam(value="start", defaultValue="0") Integer start,
			@RequestParam(value="length", defaultValue="10") Integer length) {
		
		Employee employee = getEmployee();

		return dailyRecordAction.listAll(employee.getId(), start/length, length);
		
	}
	
	/**
	 * @param number	
	 * @param size		
	 * @param model		
	 * 
	 */
	@RequestMapping(value="listOne.json")
	public Map<String, Object> listOne(@RequestParam(value="id") Integer id) {
		ResultModel result = dailyRecordAction.listOne(id);
		
		return result.toMap();
	}
	
	/**
	 * @param person
	 * @param bindingResult
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="add.json")
	public Map<String, Object> add(DailyRecordVo record, 
			BindingResult bindingResult) {
		
		if (bindingResult.hasErrors()) {
			return parseBindingResult(bindingResult);
		}
		Employee employee = getEmployee();
		
		ResultModel result = dailyRecordAction.add(employee.getId(), record);
		
		return result.toMap();
	}
	
	/**
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="remove.json")
	public Map<String, Object> remove(@RequestParam(value="id") Integer id) {
		ResultModel result = dailyRecordAction.remove(id);
		
		return result.toMap();
	}
	
	/**
	 * @param record
	 * @param bindingResult
	 * @param model
	 * @return
	 */
	@RequestMapping(value="modify.json")
	public Map<String, Object> modify(DailyRecordVo record, 
			BindingResult bindingResult) {
		
		if (bindingResult.hasErrors()) {
			return parseBindingResult(bindingResult);
		}
		ResultModel result = dailyRecordAction.modify(record);
		
		return result.toMap();
	}
}
